package com.ningmengs789.video_shooting.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ningmengs789.video_shooting.R;
import com.ningmengs789.video_shooting.adapter.VideoGridAdapter;
import com.ningmengs789.video_shooting.base.BaseActivity;
import com.ningmengs789.video_shooting.helper.ToolbarHelper;
import com.ningmengs789.video_shooting.model.LocalVideoModel;
import com.ningmengs789.video_shooting.otherCutVideo.EsayVideoEditActivity;
import com.ningmengs789.video_shooting.permissions.IPermissions1;
import com.ningmengs789.video_shooting.permissions.PermissionsUtils1;
import com.ningmengs789.video_shooting.utils.RecycleViewDivider;
import com.ningmengs789.video_shooting.utils.VideoUtil;
import com.ningmengs789.video_shooting.view.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * 相册
 */
public class VideoAlbumActivity extends BaseActivity implements VideoGridAdapter.OnItemClickListener, IPermissions1 {

    RecyclerView mRecyclerView;

    ImageView head_back;

    TextView toolbar_title;


    private List<LocalVideoModel> mLocalVideoModels = new ArrayList<>();
    private VideoGridAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_album;
    }

    @Override
    protected void initToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.setTitle(getString(R.string.video_shooting_17));
    }

    @Override
    protected void initView() {

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        head_back = findViewById(R.id.head_back);
        toolbar_title = findViewById(R.id.toolbar_title);

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        mAdapter = new VideoGridAdapter(this, mLocalVideoModels);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);

        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.color_ffffff)));


        head_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        toolbar_title.setText(getString(R.string.video_shooting_18));

        PermissionsUtils1.initPermission(getApplicationContext());

    }

    @Override
    protected void initData() {
        VideoUtil.getLocalVideoFiles(this)
                .subscribe(new Observer<ArrayList<LocalVideoModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        subscribe(d);
                    }

                    @Override
                    public void onNext(ArrayList<LocalVideoModel> localVideoModels) {
                        LocalVideoModel ll = new LocalVideoModel();
                        localVideoModels.add(0, ll);


                        mLocalVideoModels = localVideoModels;
                        mAdapter.setData(mLocalVideoModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onItemClick(int position, LocalVideoModel model) {
        if (position == 0) {
            PermissionsUtils1.onResume(this, this);
        } else {
            if ((model.getDuration() / 1000) < 60){
                setResult_(model.getVideoPath());
            }else {
                Intent intent = new Intent(this, EsayVideoEditActivity.class);
                intent.putExtra("videoPath", model.getVideoPath());
                startActivityForResult(intent, 20003);
            }
        }
    }

    @Override
    public void allPermissions() {
        Intent intent = new Intent(this, VideoCameraActivity.class);
        startActivityForResult(intent, 20003);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == 20003) {
            String path = data.getStringExtra("videoPath");
            if (!TextUtils.isEmpty(path)) {
                setResult_(path);
            }
        }
    }

    private void setResult_(String videoPath){
        setResult(20002, new Intent().putExtra("videoPath", videoPath));
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionsUtils1.onRequestPermissionsResult(requestCode, permissions, grantResults, this, this, true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocalVideoModels = null;
    }


}
